import { dialog } from 'electron';
import fs from 'fs';
import { SEND } from '../../events/event.send';

export default (e: Electron.IpcMainEvent) => {
  dialog
    .showOpenDialog({
      properties: ['openFile'],
      filters: [
        {
          name: '文本文件',
          extensions: ['txt'],
        },
      ],
    })
    .then((result) => {
      if (!result.canceled) {
        /* 读取文件内容 */
        const path = result.filePaths[0];
        const content = fs.readFileSync(path, 'utf-8');
        console.log('读取文件', content);

        e.sender.send(SEND.READ_FILE, { path, content });
      }
    });
};
